package com.controller.system.order;

import com.entity.system.AgentNew;
import com.entity.system.StaticRole;
import com.entity.system.UserInfo;
import com.service.system.agent.AgentService;
import com.service.system.order.OrderPayService;
import com.service.system.order.OrderService;
import com.util.Const;
import com.util.LogUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 订单
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderPayService orderPayService;

    @Autowired
    AgentService agentService;

    /**
     * 根据登录用户查询订单
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/findOrderByUser", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> findOrderByUser(HttpServletRequest request) {
        String pageSize = request.getParameter("pageSize");
        String pageNum = request.getParameter("pageNum");
        String startTiem = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String phone = request.getParameter("phone");
        String orderState = request.getParameter("orderState");
        //shiro管理的session
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();

        UserInfo user = (UserInfo) session.getAttribute(Const.SESSION_USER);

        return orderService.findOrderByUser(user.getId(), pageSize, pageNum, startTiem, endTime, phone,orderState);
    }

    /**
     * 订单列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/fundOrderList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> fundOrderList(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", request.getParameter("pageSize"));
        map.put("pageNum", request.getParameter("pageNum"));
        if(StringUtils.isNotEmpty(request.getParameter("startTime"))){
            map.put("startTime",request.getParameter("startTime"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("endTime"))){
            map.put("endTime",request.getParameter("endTime"));
        }
        if (StringUtils.isNotEmpty(request.getParameter("queryKey"))) {
            // 根据订单流水号查询
            map.put("orderNo",request.getParameter("queryKey"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("state"))){
            map.put("state",request.getParameter("state"));
        }
        return orderService.fundOrderList(map);
    }

    /**
     * 支付列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/fundOrderPayList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> fundOrderPayList(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", request.getParameter("pageSize"));
        map.put("pageNum", request.getParameter("pageNum"));
        if(StringUtils.isNotEmpty(request.getParameter("startTime"))){
            map.put("startTime",request.getParameter("startTime"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("endTime"))){
            map.put("endTime",request.getParameter("endTime"));
        }
        if (StringUtils.isNotEmpty(request.getParameter("queryKey"))) {
            // 根据订单号查询
            map.put("orderNo",request.getParameter("queryKey"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("state"))){
            map.put("state",request.getParameter("state"));
        }
        return orderPayService.fundOrderPayList(map);
    }

    /**
     * 查询提现审核列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/findWithdrawalList",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> findWithdrawalList(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        map.put("pageSize", request.getParameter("pageSize"));
        map.put("pageNum", request.getParameter("pageNum"));
        if(StringUtils.isNotEmpty(request.getParameter("startTime"))){
            map.put("startTime",request.getParameter("startTime"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("endTime"))){
            map.put("endTime",request.getParameter("endTime"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("isAuditPass"))){
            map.put("isAuditPass",request.getParameter("isAuditPass"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("state"))){
            map.put("state",request.getParameter("state"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("cashFlowType"))){
            map.put("type",request.getParameter("cashFlowType"));
        }else {
            map.put("notType",request.getParameter("notType"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("phone"))){
            map.put("phone",request.getParameter("phone"));
        }

        AgentNew an = null;
        if(StringUtils.isNotEmpty(request.getParameter("orderType"))){

            //shiro管理的session
            Subject currentUser = SecurityUtils.getSubject();
            Session session = currentUser.getSession();
            Map<String, Object> map1 = (Map<String, Object>) session.getAttribute(Const.SESSION_allmenuList);
            Map<String, Object> map2 = new HashMap<>();

            if (null == map1) {
                map2.put("code", Const.ERROR_CODE);
                map2.put("message", "查询失败");
                return map2;
            } else {
                //当前登录用户角色
                StaticRole sr = (StaticRole) map1.get("staticRole");
                if (sr != null && sr.getRoleCode().equals("VERSION_NEW_ROLE_AGENT")) {
                    UserInfo user = (UserInfo) session.getAttribute(Const.SESSION_USER);
                    //代理商信息
                    an = agentService.getAgentNewByUserId(user.getId());
                    if (an == null) {
                        LogUtil.info("*************你不是代理商*******************");
                        map2.put("code", Const.ERROR_CODE);
                        map2.put("message", "查询失败");
                        return map2;
                    }
                }
            }
        }
        return orderService.findWithdrawalList(map,an);
    }

    /**
     * 提现审核
     * @param request
     * @return
     */
    @RequestMapping(value = "/withdrawaAudit",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> withdrawaAudit(HttpServletRequest request){
        String id = request.getParameter("id"); // 提现ID
        String type = request.getParameter("auditType"); //审核类型  1.通过  2.不通过
        String content = request.getParameter("content");
        return orderService.withdrawaAudit(id,type,content);
    }
}
